//
//  UserCommentCell.m
//  danpin
//
//  Created by chuangjia on 2/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "UserCommentCell.h"

#import "ZUIImageView.h"
@interface UserCommentCell ()
{
    
}
@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)RTLabel *timeLabel;
@property(nonatomic,strong)RTLabel *contentLabel;

@property(nonatomic,strong)UIImageView *lineView;
@property(nonatomic,strong)UIView *wareView;
@property(nonatomic,strong)ZUIImageView *goodsView;
@property(nonatomic,strong)UIImageView *shaidanView;
@property(nonatomic,strong)RTLabel *gTitleLabel;
@property(nonatomic,strong)UILabel *ppriceLable;
@end
@implementation UserCommentCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.layer.cornerRadius=PHOTO_FRAME_WIDTH*2;
        _imagesView.layer.masksToBounds = YES;
        [self.contentView addSubview:_imagesView];
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = UIColorFromRGB(0x285790);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = FONT(14);
        _titleLabel.lineSpacing =LSpacing;
        [self.contentView addSubview:_titleLabel];
        _timeLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, 250,20)];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.textColor = UIColorFromRGB(0xcccccc);
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = FONT(12);
        _timeLabel.lineSpacing =LSpacing;
        [self.contentView addSubview:_timeLabel];
        
        _contentLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6,0)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.textColor = [UIColor darkGrayColor];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.font = FONT(14);
        _contentLabel.lineSpacing =LSpacing;
        [self.contentView addSubview:_contentLabel];
        
        _wareView =[[UIView alloc] initWithFrame:CGRectZero];
        _wareView.backgroundColor =UIColorFromRGB(0xf7f7f7);
        [self.contentView addSubview:_wareView];
        
        _goodsView =[[ZUIImageView alloc] initWithFrame:CGRectZero];
        _goodsView.contentMode = UIViewContentModeScaleAspectFit;
        _goodsView.backgroundColor =UIColorFromRGB(0xffffff);
        [_wareView addSubview:_goodsView];
        //
        _shaidanView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _shaidanView.image =[UIImage imageNamed:@"shaidan_icon.png"];
        [_goodsView addSubview:_shaidanView];
        
        _gTitleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*13,0)];
        _gTitleLabel.backgroundColor = [UIColor clearColor];
        _gTitleLabel.textColor = [UIColor darkGrayColor];
        _gTitleLabel.textAlignment = NSTextAlignmentLeft;
        _gTitleLabel.font = [UIFont boldSystemFontOfSize:14];
        _gTitleLabel.lineSpacing =LSpacing;
        [_wareView addSubview:_gTitleLabel];
        
        _ppriceLable =[[UILabel alloc] initWithFrame:CGRectZero];
        _ppriceLable.backgroundColor = [UIColor clearColor];
        _ppriceLable.textColor = [UIColor grayColor];
        _ppriceLable.textAlignment = NSTextAlignmentLeft;
        _ppriceLable.font = FONT(12);
        [_wareView addSubview:_ppriceLable];
        
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [self.contentView addSubview:_lineView];
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.other=nil;
    self.key=nil;
    self.indexPath=nil;
    
    self.imagesView.image=nil;
    [self.imageView removeFromSuperview];
    self.imagesView=nil;
    [self.titleLabel removeFromSuperview];
    self.titleLabel=nil;
    [self.wareView removeFromSuperview];
    self.wareView=nil;
    [self.timeLabel removeFromSuperview];
    self.timeLabel=nil;
    [self.contentLabel removeFromSuperview];
    self.contentLabel=nil;
    [self.goodsView removeFromSuperview];
    self.goodsView =nil;
    [self.shaidanView removeFromSuperview];
    self.shaidanView=nil;
    [self.gTitleLabel removeFromSuperview];
    self.gTitleLabel=nil;
    [self.lineView removeFromSuperview];
    self.lineView =nil;
    UIView* currentView = [self.contentView viewWithTag:10000];
    [currentView removeFromSuperview];
    UIView* currentView1 = [self.contentView viewWithTag:10001];
    [currentView1 removeFromSuperview];

     DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    self.lineView.frame =CGRectMake(0,self.bounds.size.height-1, DEVICE_WIDTH, 1);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath =indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    CGFloat fTop = PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    _imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop, PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    NSString * strURL =[NSString stringWithFormat:@"%@", [dict objectForKey:@"largeFace"]];
    [_imagesView sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
    NSString *test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"nick_name"]] replaceNullString];
    [_titleLabel setText:test];
    CGSize optimumSize = [_titleLabel optimumSize];
    
    _titleLabel.frame =CGRectMake(_imagesView.frame.origin.x+_imagesView.frame.size.width+PHOTO_FRAME_WIDTH, _imagesView.frame.origin.y,optimumSize.width, optimumSize.height);
    _titleLabel.text =test;
    //
    NSString *test1 =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"comment_dateinfo"]] replaceNullString];
    [_timeLabel setText:test1];
    CGSize optimumSize2 = [_timeLabel optimumSize];
    _timeLabel.frame=CGRectMake(DEVICE_WIDTH-optimumSize2.width-PHOTO_FRAME_WIDTH,_titleLabel.frame.origin.y,optimumSize2.width, optimumSize2.height);
    _timeLabel.text =test1;
    //
    NSString *content =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    [_contentLabel setText:content];
    CGSize optimumSize1 = [_contentLabel optimumSize];
    
    _contentLabel.frame =CGRectMake(_imagesView.frame.origin.x+_imagesView.frame.size.width+PHOTO_FRAME_WIDTH, _titleLabel.frame.origin.y+_titleLabel.frame.size.height+PHOTO_FRAME_WIDTH, DEVICE_WIDTH-(_imagesView.frame.origin.x+_imagesView.frame.size.width+PHOTO_FRAME_WIDTH)-PHOTO_FRAME_WIDTH, optimumSize1.height);
    _contentLabel.text =content;
    //
    fTop = _contentLabel.frame.origin.y+_contentLabel.frame.size.height+PHOTO_FRAME_WIDTH;
    
    //
    id ware = [dict objectForKey:@"ware"];
    if (ware && [ware isKindOfClass:[NSDictionary class]]) {
        int is_shaidan =[[ware objectForKey:@"is_shaidan"] intValue];
        if (is_shaidan==10) {
            UIView *tempView =[self bangdanView:ware y:fTop];
            [self.contentView addSubview:tempView];
//            fTop=fTop +tempView.frame.size.height+PHOTO_FRAME_WIDTH;
        }else{
            UIView *tempView =[self danpinView:ware y:fTop];
            [self.contentView addSubview:tempView];
//            fTop=fTop +tempView.frame.size.height+PHOTO_FRAME_WIDTH;
        }
    }
    
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}


//todo： 这个函数太卡了。 严重卡顿
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary *)model;
    float hight1 =0;
    NSString *test =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"nick_name"]] replaceNullString];
    if (test.length>0) {
        test =[NSString stringWithFormat:@"%@",test];
        
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = LSpacing;
        [textLabel setText:test];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        CGFloat hh =optimumSize1.height;
        if (hh<20) {
            hh=20;
        }
        hight1 =hh;
    }
    float hight2 =0;
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"%@",text];
        
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH)*6,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = LSpacing;
        [textLabel setText:text];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        CGFloat hh =optimumSize1.height;
        if (hh<20) {
            hh=20;
        }
        hight2 = hh;
    }
     //
    id ware = [dict objectForKey:@"ware"];
    if (ware && [ware isKindOfClass:[NSDictionary class]]) {
        int is_shaidan =[[ware objectForKey:@"is_shaidan"] intValue];
        if (is_shaidan==10)
        {
            float fTop = PHOTO_FRAME_WIDTH;
            //
            
            NSString *skuname  =[[NSString stringWithFormat:@"%@", [ware objectForKey:@"title"]] replaceNullString];
            if (skuname.length>0) {
                RTLabel *skuNameLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4,0)];
                skuNameLabel.backgroundColor = [UIColor clearColor];
                skuNameLabel.textColor = [UIColor darkGrayColor];
                skuNameLabel.textAlignment = NSTextAlignmentLeft;
                skuNameLabel.font = [UIFont boldSystemFontOfSize:16];
                skuNameLabel.lineSpacing = LSpacing;
                [skuNameLabel setText:[NSString stringWithFormat:@"%@",skuname]];
                CGSize optimumSize = [skuNameLabel optimumSize];
                CGFloat hh =optimumSize.height;
                if (hh<20) {
                    hh=20;
                }
                fTop = fTop+hh;
                skuNameLabel=nil;
            }
            
            fTop = fTop+PHOTO_FRAME_WIDTH*3;
            //
            
            NSString *text =[[NSString stringWithFormat:@"%@", [ware objectForKey:@"text"]] replaceNullString];
            if (text.length>0) {
                RTLabel *skuNameContent =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4,0)];
                skuNameContent.backgroundColor = [UIColor clearColor];
                skuNameContent.textColor = [UIColor darkGrayColor];
                skuNameContent.textAlignment = NSTextAlignmentLeft;
                skuNameContent.font = FONT(14);
                skuNameContent.lineSpacing =LSpacing;
                text =[NSString stringWithFormat:@"%@",text];
                [skuNameContent setText:text];
                CGSize optimumSize11 = [skuNameContent optimumSize];
                fTop = fTop+optimumSize11.height;
                skuNameContent=nil;
            }
            fTop = fTop+PHOTO_FRAME_WIDTH;
            
            id comments = [ware objectForKey:@"detailPicURL"];
            if (comments && [comments isKindOfClass:[NSArray class]]&&[comments count]>0) {
                fTop=fTop+((DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4)/3)+PHOTO_FRAME_WIDTH;
            }
            
            hight2 =fTop+hight2+PHOTO_FRAME_WIDTH;
            
        }else{
            hight2 = hight2 +PHOTO_FRAME_WIDTH*10+PHOTO_TIME_PADDING;
        }
    }

    return PHOTO_FRAME_WIDTH*4+hight1+hight2;
}
-(UIView*)danpinView:(NSDictionary *)dict y:(CGFloat)y
{
    UIView* currentView = [self.contentView viewWithTag:10000];
    [currentView removeFromSuperview];
    UIView *wareView =[[UIView alloc] initWithFrame:CGRectZero];
    wareView.tag = 10000;
    wareView.frame =CGRectMake(PHOTO_FRAME_WIDTH, y, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*10);
    wareView.backgroundColor =UIColorFromRGB(0xf7f7f7);
    
    ZUIImageView *goodsView =[[ZUIImageView alloc] initWithFrame:CGRectZero];
    goodsView.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*8, PHOTO_FRAME_WIDTH*8);
    goodsView.contentMode = UIViewContentModeScaleAspectFit;
    goodsView.backgroundColor =UIColorFromRGB(0xffffff);
    NSString *ustr =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"largePicURL"]] replaceNullString];
    goodsView.imageUrl = ustr;
    goodsView.placeholderImage =[UIImage imageNamed:@"listcellimage.png"];
    
    [wareView addSubview:goodsView];
 
    //
    RTLabel *gTitleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*13,0)];
    gTitleLabel.backgroundColor = [UIColor clearColor];
    gTitleLabel.textColor = [UIColor darkGrayColor];
    gTitleLabel.textAlignment = NSTextAlignmentLeft;
    gTitleLabel.font = [UIFont boldSystemFontOfSize:14];
    gTitleLabel.lineSpacing =LSpacing;
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    [gTitleLabel setText:brand];
    CGSize optimumSizeg = [gTitleLabel optimumSize];
    gTitleLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH*10, PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*13, optimumSizeg.height);
    [wareView addSubview:gTitleLabel];
   
    
    UILabel *ppriceLable =[[UILabel alloc] initWithFrame:CGRectZero];
    ppriceLable.backgroundColor = [UIColor clearColor];
    ppriceLable.textColor = [UIColor grayColor];
    ppriceLable.textAlignment = NSTextAlignmentLeft;
    ppriceLable.font = FONT(12);
    ppriceLable.frame=CGRectMake(PHOTO_FRAME_WIDTH*10,goodsView.frame.origin.y+goodsView.frame.size.height-20,  DEVICE_WIDTH-PHOTO_FRAME_WIDTH*11, 20);
    ppriceLable.text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"price_str"]] replaceNullString];
    [wareView addSubview:ppriceLable];
    
    return wareView;
}
-(UIView*)bangdanView:(NSDictionary *)dict y:(CGFloat)y
{
    UIView* currentView = [self.contentView viewWithTag:10001];
    [currentView removeFromSuperview];
    UIView *wareView =[[UIView alloc] initWithFrame:CGRectZero];
    wareView.tag = 10001;
    wareView.backgroundColor =UIColorFromRGB(0xf7f7f7);
    float fTop = 0;
    fTop += PHOTO_FRAME_WIDTH;
    //
    RTLabel *skuNameLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4,0)];
    skuNameLabel.backgroundColor = [UIColor clearColor];
    skuNameLabel.textColor = [UIColor darkGrayColor];
    skuNameLabel.textAlignment = NSTextAlignmentLeft;
    skuNameLabel.font = [UIFont boldSystemFontOfSize:16];
    skuNameLabel.lineSpacing = LSpacing;
    NSString *skuname=[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if (skuname.length>0) {
        [skuNameLabel setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [skuNameLabel optimumSize];
        CGFloat hh =optimumSize.height;
        if (hh<20) {
            hh=20;
        }
        skuNameLabel.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, hh);
        
        fTop = fTop+hh+PHOTO_FRAME_WIDTH;
    }
    [wareView addSubview:skuNameLabel];
    skuNameLabel=nil;
    //
    UIImageView*iconView =[[UIImageView alloc] initWithFrame:CGRectZero];
    iconView.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop, 32, 16);
    iconView.image =[UIImage imageNamed:@"bangdan.png"];
    [wareView addSubview:iconView];
    iconView=nil;
    //
    RTLabel *cateLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4,0)];
    cateLabel.backgroundColor = [UIColor clearColor];
    cateLabel.textColor = UIColorFromRGB(0xe999999);
    cateLabel.textAlignment = NSTextAlignmentLeft;
    cateLabel.font = FONT(14);
    cateLabel.lineSpacing =LSpacing;
    NSString *recommend_user =[NSString stringWithFormat:@"%@个推荐 / %@个人参与", [dict objectForKey:@"recommend_user"],[dict objectForKey:@"push_user"]];
    cateLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH*2+32,fTop,DEVICE_WIDTH - 32-PHOTO_FRAME_WIDTH*3, PHOTO_FRAME_WIDTH*2);
    cateLabel.text =recommend_user;
    [wareView addSubview:cateLabel];
    cateLabel=nil;
    //
    fTop = fTop+PHOTO_FRAME_WIDTH*3;
    RTLabel *skuNameContent =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4,0)];
    skuNameContent.backgroundColor = [UIColor clearColor];
    skuNameContent.textColor = [UIColor darkGrayColor];
    skuNameContent.textAlignment = NSTextAlignmentLeft;
    skuNameContent.font = FONT(14);
    skuNameContent.lineSpacing =LSpacing;
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"%@",text];
        [skuNameContent setText:text];
        CGSize optimumSize1 = [skuNameContent optimumSize];
        skuNameContent.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, optimumSize1.height);
        fTop = fTop+optimumSize1.height+PHOTO_FRAME_WIDTH;
    }
    [wareView addSubview:skuNameContent];
    skuNameContent=nil;
    UIView *commentView = [[UIView alloc] initWithFrame:CGRectZero];
    commentView.backgroundColor =[UIColor clearColor];
    id comments = [dict objectForKey:@"detailPicURL"];
    if (comments && [comments isKindOfClass:[NSArray class]]&&[comments count]>0) {
        NSMutableArray *cmtArray =[NSMutableArray arrayWithArray:comments];
        int cmtCount =(int)[cmtArray count];
        if(cmtCount>=3)
        {
            cmtCount = 3;
        }
        if (cmtCount>0) {
            int totalloc = 3;
            CGFloat appvieww=((DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4)/3);
            CGFloat margin=(DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4-totalloc*appvieww)/(totalloc+1);
            for (int i =0 ; i<cmtCount; i++) {
                int row=i/totalloc;//行号
                //1/3=0,2/3=0,3/3=1;
                int loc=i%totalloc;//列号
                CGFloat appviewx=margin+(margin+appvieww)*loc;
                CGFloat appviewy=margin+(margin+appvieww)*row;
                
                NSDictionary *cDict = [cmtArray objectAtIndex:i];
                NSString *urlstr =[[NSString stringWithFormat:@"%@",[cDict objectForKey:@"url"]] replaceNullString];
                ZUIImageView *imageView =[[ZUIImageView alloc] initWithFrame:CGRectZero];
                imageView.frame = CGRectMake(appviewx,appviewy, appvieww ,appvieww);
                imageView.backgroundColor =[UIColor whiteColor];
                imageView.imageUrl = urlstr;
                imageView.placeholderImage =[UIImage imageNamed:@"image.png"];
                [commentView addSubview:imageView];
                imageView=nil;
            }
            
            commentView.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, appvieww+PHOTO_TIME_PADDING);
            fTop =fTop +commentView.frame.size.height;
        }
    }
    [wareView addSubview:commentView];
    commentView=nil;
    wareView.frame =CGRectMake(PHOTO_FRAME_WIDTH, y, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, fTop);
//    DLog(@"=wareView.frame=======%f====%f",y,fTop);
    return wareView;
}

@end
